-
-
Notifications
You must be signed in to change notification settings - Fork 31.6k
gh-131952: Add color to the json
CLI
#132126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How much of a performance impact does this have?
It doesn't matter here. |
I think we might not need that, |
json.tool
CLI.json.tool
CLI
What about CLIs? they would be broken if data is piped? |
AFAIU they wouldn't (and local testing confirms that), see this comment from Hugo: #131952 (comment) |
@tomasr8 thank you for taking the time to look into it. couple of notes regarding the keys 1) could you make them a different color from the string values? 2) could you make them bold? this is what jq does and i think it provides a good ux https://static1.howtogeekimages.com/wordpress/wp-content/uploads/2020/01/5-7.png |
That should be in theory possible, we can distinguish keys from string literals since keys are always followed by |
Lib/json/tool.py
Outdated
with_colors = can_colorize() | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This variable isn't doing much, shall we just call the function directly?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I may have been too cautious here. Without the variable, the function would be called in a loop and I was worried the result could change in between invocations, though that is really unlikely. If you prefer I can remove it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, I applied your other suggestion which already removed it so never mind 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, that's a good point: there could be a lot of objs
as JSONL files can be quite long. Let's add with_colors
back, but just outside the loop.
Misc/NEWS.d/next/Library/2025-04-05-16-05-34.gh-issue-131952.HX6gCX.rst
Outdated
Show resolved
Hide resolved
json.tool
CLIjson
CLI
Co-authored-by: Adam Turner <[email protected]>
Here's a couple more variants. The first is the current and uses the same colors as Lukasz's syntax highlight PR. The last one uses a different color for the keys. ![]() ![]() ![]() ![]() ![]() ![]() |
The green and cyan colours here look bad on the white background, which is the default for Windows Terminal, PyCharm's in-built terminal, etc. Could you darken them? |
I also prefer the bold version, thanks. A |
Thanks for your feedback! I think it looks much better now :) I added the bold colors and updated tests. Would you mind taking another look? |
i like the colors. one small request: could you bold the keys and leave the values regular? that's how jq does it https://static1.howtogeekimages.com/wordpress/wp-content/uploads/2020/01/5-7.png |
I think I prefer the bold colors. In my opinion, they are more legible when bold, especially with the light theme. |
in the last pair of screenshots, i see only the values as bold. the keys look regular. my proposal was to invert those. or do you want to make the keys bold, too? i think that could work |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
A
for key in _colors: | ||
if m := match.group(key): | ||
color = _colors[key] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for key in _colors: | |
if m := match.group(key): | |
color = _colors[key] | |
for key, color in _colors.items(): | |
if m := match.group(key): |
Please could you also add this to What's New, and mention the colour env vars, like the other similar entries? |
Example:
I'm open to changing the colors. For now it's green for strings, yellow for numbers and cyan for true/false/null.